package zhjb.temp.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class DBInit {
	private String driver;
	private String user;
	private String password;
	private String url;
	private List<String> clazzs;
	private static DBInit instance;
	
	/**
	 * 获取实例
	 * @return
	 */
	public static DBInit getDBInit() {
		return instance;
	}

	public void init() throws Exception {
		for (String clazz : clazzs) {
			System.out.println("注册：" + clazz);
			Context.registe(Class.forName(clazz));
		}
		instance = this;
	}

	/**
	 * 获取新的connection
	 * 
	 * @return
	 */
	public Connection getNewConnection() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<String> getClazzs() {
		return clazzs;
	}

	public void setClazzs(List<String> clazzs) {
		this.clazzs = clazzs;
	}
}
